home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp95 / freyja13.exe / lha / INI.C < prev    next >
C/C++ Source or Header  |  1992-03-22  |  7KB  |  294 lines

  1. /* INI.C -- Routines to Load the Initialization File
  2.  
  3.     Written March 1991 by Craig A. Finseth
  4.     Copyright 1991 by Craig A. Finseth
  5. */
  6.  
  7. #include "freyja.h"
  8.  
  9. #define PAGEK        (PAGESIZE / 1024)
  10.  
  11. /* parsing and initialization tables */
  12.  
  13. #define NUM_IC        (sizeof(ic) / sizeof(ic[0]))
  14. static struct ini_char {
  15.     char *tok;    /* token to match */
  16.     char *var;    /* variable to store into */
  17.     char def;    /* default value */
  18.     char *valid;    /* valid characters */
  19.     } ic[] = {
  20. #if defined(UNIX)
  21. { "SCR", &c.g.screen_type,    'V',    "@VT" },    /* must be first */
  22. { "SCS", &c.g.screen_size,    '@',    "@" },
  23. { "KEY", &c.g.key_type,        'C',    "@C" },
  24. { "SPE", &c.g.special,        '@',    "@" },
  25. { "PA1", &c.g.parameter_1,    '@',    "@" },
  26. #else
  27. #if defined(MSDOS)
  28. { "SCR", &c.g.screen_type,    'V',    "@VBM" },    /* must be first */
  29. { "SCS", &c.g.screen_size,    '@',    "@J" },
  30. { "KEY", &c.g.key_type,        'C',    "@CSBJ" },
  31. { "SPE", &c.g.special,        '@',    "@J" },
  32. { "PA1", &c.g.parameter_1,    '@',    "@P" },
  33. #else
  34. { "SCR", &c.g.screen_type,    'V',    "@V" },        /* must be first */
  35. { "SCS", &c.g.screen_size,    '@',    "@" },
  36. { "KEY", &c.g.key_type,        'C',    "@C" },
  37. { "SPE", &c.g.special,        '@',    "@" },
  38. { "PA1", &c.g.parameter_1,    '@',    "@" },
  39. #endif
  40. #endif
  41. { "MET", &c.g.meta_handle,    'B',    "@BIM" },
  42. { "FIL", &c.d.fill,        'N',    "@NFW" } };
  43.  
  44. #define NUM_IF        (sizeof(ifl) / sizeof(ifl[0]))
  45. static struct ini_flag {
  46.     char *tok;    /* token to match */
  47.     FLAG *var;    /* variable to store into */
  48.     FLAG def;    /* default value */
  49.     } ifl[] = {
  50. { "WRA", &c.g.wrap_allowed,    TRUE },
  51. { "VIS", &c.g.vis_gray,        FALSE },
  52. { "USE", &c.g.use_caret,    TRUE } };
  53.  
  54. #define NUM_II        (sizeof(ii) / sizeof(ii[0]))
  55. static struct ini_int {
  56.     char *tok;    /* token to match */
  57.     int *var;    /* variable to store into */
  58.     int def;    /* default value */
  59.     } ii[] = {
  60. { "SWA", &c.g.swap_size,    128 },
  61. { "ESC", &c.g.ESC_swap,        27 },
  62. { "LEF", &c.d.left_margin,    0 },
  63. { "RIG", &c.d.right_margin,    70 },
  64. { "TAB", &c.d.tab_spacing,    8 } };
  65.  
  66.  
  67. static char *buffer;
  68. static char *bufptr;
  69. static int buflen;
  70. static int line;
  71. static int fd;
  72.  
  73. int Ini_Next();        /* int fd */
  74. FLAG Ini_Token();    /* char *token, int len */
  75.  
  76. /* ------------------------------------------------------------ */
  77.  
  78. /* Load the configuration information.  If no file, use defaults.  If
  79. an error, print a message and use defaults. */
  80.  
  81. FLAG
  82. IniLoad(s, z)
  83.     char s;
  84.     int z;
  85.     {
  86.     char fname[FNAMEMAX];
  87.     char buf[BUFFSIZE];
  88.     char tok1[32];
  89.     char tok2[32];
  90.     char chr;
  91.     int cnt;
  92.     FLAG handled;
  93.  
  94. /* set defaults */
  95.  
  96.     for (cnt = 0; cnt < NUM_IC; cnt++) {
  97.         *ic[cnt].var = ic[cnt].def;
  98.         }
  99.     for (cnt = 0; cnt < NUM_IF; cnt++) {
  100.         *ifl[cnt].var = ifl[cnt].def;
  101.         }
  102.     for (cnt = 0; cnt < NUM_II; cnt++) {
  103.         *ii[cnt].var = ii[cnt].def;
  104.         }
  105.  
  106.     s = xtoupper(s);
  107.     if (*sindex(ic[0].valid, s) == NUL) {
  108. #if defined(SYSMGR)
  109.         xsprintf(buf, "option value '%c' is not one of %s.",
  110.             s, ic[0].valid);
  111.         JMsg(buf);
  112. #else
  113.         xeprintf("option value '%c' is not one of %s.\r\n",
  114.             s, ic[0].valid);
  115. #endif
  116.         return(FALSE);
  117.         }
  118.     c.g.screen_type = s;
  119.     c.g.swap_size = z;
  120.  
  121. /* process the file */
  122.  
  123.     *fname = NUL;
  124.     if ((fd = FPathOpen(INI_FILENAME, fname)) < 0) goto OK;
  125.  
  126.     buffer = buf;
  127.     bufptr = buffer;
  128.     buflen = -1;
  129.     line = 1;
  130.  
  131.     while (Ini_Token(tok1, sizeof(tok1)) && Ini_Token(tok2, sizeof(tok2))){
  132.         handled = FALSE;
  133.  
  134.         for (cnt = 0; cnt < NUM_IC; cnt++) {
  135.             if (!strequ(tok1, ic[cnt].tok)) continue;
  136.             *ic[cnt].var = xtoupper(*tok2);
  137.             if (*sindex(ic[cnt].valid, *ic[cnt].var) == NUL) {
  138. #if defined(SYSMGR)
  139.                 xsprintf(buf, "%s value '%c' is not one of %s.",
  140.                     ic[cnt].tok, *tok2, ic[cnt].valid);
  141.                 JMsg(buf);
  142. #else
  143.                 xeprintf("%s value '%c' is not one of %s.\r\n",
  144.                     ic[cnt].tok, *tok2, ic[cnt].valid);
  145. #endif
  146.                 }
  147.             else    handled = TRUE;
  148.             }
  149.  
  150.         for (cnt = 0; cnt < NUM_IF; cnt++) {
  151.             if (!strequ(tok1, ifl[cnt].tok)) continue;
  152.             chr = xtoupper(*tok2);
  153.             if (*sindex("YN", chr) == NUL) {
  154. #if defined(SYSMGR)
  155.                 xsprintf(buf, "%s value '%s' is not one of Y or N.",
  156.                     ifl[cnt].tok, tok2);
  157.                 JMsg(buf);
  158. #else
  159.                 xeprintf("%s value '%s' is not one of Y or N.\r\n",
  160.                     ifl[cnt].tok, tok2);
  161. #endif
  162.                 }
  163.             else    {
  164.                 *ifl[cnt].var = chr == 'Y';
  165.                 handled = TRUE;
  166.                 }
  167.             }
  168.  
  169.         for (cnt = 0; cnt < NUM_II; cnt++) {
  170.             if (!strequ(tok1, ii[cnt].tok)) continue;
  171.             if (!SToN(tok2, ii[cnt].var, 10)) {
  172. #if defined(SYSMGR)
  173.                 xsprintf(buf, "%s value '%s' is not a number.",
  174.                     ii[cnt].tok, tok2);
  175.                 JMsg(buf);
  176. #else
  177.                 xeprintf("%s value '%s' is not a number.\r\n",
  178.                     ii[cnt].tok, tok2);
  179. #endif
  180.                 }
  181.             else    {
  182.                 if (*ii[cnt].var < 0) *ii[cnt].var = 0;
  183.                 handled = TRUE;
  184.                 }
  185.             }
  186.  
  187.         if (!handled) {
  188. #if defined(SYSMGR)
  189.             xsprintf(buf, "Problem on line %d of ini file %s.\r\n",
  190.                 line, fname);
  191.             JMsg(buf);
  192. #else
  193.             xeprintf("Problem on line %d of ini file %s.\r\n",
  194.                 line, fname);
  195. #endif
  196.             close(fd);
  197.             return(FALSE);
  198.             }
  199.         }
  200.     close(fd);
  201.  
  202. /* handle consistency */
  203.  
  204. OK:
  205.  
  206.     for (cnt = 0; cnt < NUM_IC; cnt++) {
  207.         if (*ic[cnt].var == '@') *ic[cnt].var = ic[cnt].def;
  208.         }
  209.  
  210.     if (c.g.swap_size < 16) c.g.swap_size = 16;
  211.     if (c.g.swap_size > 512) c.g.swap_size = 512;
  212.     c.g.swap_size = (c.g.swap_size + PAGEK - 1) / PAGEK;
  213.     c.g.swap_size *= PAGEK;
  214.  
  215. #if defined(MSDOS)
  216. #if defined(SYSMGR)
  217.     c.g.special = 'J';
  218.     c.g.screen_type = 'M';
  219.     c.g.screen_size = 'J';
  220.     c.g.key_type = 'J';
  221. #else
  222.     if (c.g.special == 'J') {
  223.         c.g.screen_type = 'M';
  224.         c.g.screen_size = 'J';
  225.         c.g.key_type = 'J';
  226.         }
  227. #endif
  228. #endif
  229.  
  230.     WFixup(&c.d);
  231.     return(TRUE);
  232.     }
  233.  
  234.  
  235. /* ------------------------------------------------------------ */
  236.  
  237. /* Read and return the next character. */
  238.  
  239. int
  240. Ini_Next()
  241.     {
  242.     if (bufptr >= &buffer[buflen]) {
  243.         buflen = read(fd, buffer, sizeof(buffer));
  244.         if (buflen <= 0) return(EOF);
  245.         bufptr = buffer;
  246.         }
  247.     if (*bufptr == NL) line++;
  248.     return(*bufptr++ & 0xFF);
  249.     }
  250.  
  251.  
  252. /* ------------------------------------------------------------ */
  253.  
  254. /* Read and return one token. */
  255.  
  256. FLAG
  257. Ini_Token(token, len)
  258.     char *token;
  259.     int len;
  260.     {
  261.     int c;
  262.  
  263. /* skip whitespace */
  264.  
  265.     do    {
  266.         c = Ini_Next();
  267.         if (c == '#') {
  268.             do    {
  269.                 c = Ini_Next();
  270.                 } while (c != EOF && c != NL);
  271.             }
  272.         } while (xisgray(c));
  273.  
  274. /* copy token */
  275.  
  276.     while (--len > 0 && !xisgray(c)) {
  277.         *token++ = c;
  278.         c = Ini_Next();
  279.         }
  280.     *token = NUL;
  281.  
  282. /* check for trailing comment */
  283.  
  284.     if (c == '#') {
  285.         do    {
  286.             c = Ini_Next();
  287.             } while (c != EOF && c != NL);
  288.         }
  289.     return(c != EOF);
  290.     }
  291.  
  292.  
  293. /* end of INI.C -- Routines to Load the Initialization File */
  294.